package com.doudou.collection.controller;

import com.doudou.collection.pojo.dto.*;
import com.doudou.collection.pojo.entity.UserDetail;
import com.doudou.collection.response.ResultVO;
import com.doudou.collection.response.StatusCode;
import com.doudou.collection.security.CustomUserDetails;
import com.doudou.collection.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName UserController<br>
 * @Description 用户管理模块<br>
 * @Author 李浩文<br>
 * @Date 2023/5/31 20:05<br>
 * @Version 1.0<br>
 */
@Api(tags = "01.用户管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/reg/{type}")
    @ApiOperation("注册功能")
    public ResultVO reg(@RequestBody UserRegDTO userRegDTO, @PathVariable Integer type) {
        log.debug("正在处理注册请求");

        switch (type) {
            case 1: // type为1时 处理用户名密码登录请求
                return userService.addNewByUsername(userRegDTO.getUsername(), userRegDTO.getPassword());

            case 2: // type为2时 处理手机号密码登录请求
                return userService.addNewByPhone(userRegDTO.getPhone(), userRegDTO.getPassword());

            case 3: // type为3时 处理邮箱密码登录请求
                return userService.addNewByEmail(userRegDTO.getEmail(), userRegDTO.getPassword());

            case 4: // type为4时 处理手机号验证码登录
                return userService.addNewByPhoneAndCode(userRegDTO.getPhone(), userRegDTO.getCode());

            case 5: // type为4时 处理邮箱验证码登录
                return userService.addNewByEmailAndCode(userRegDTO.getEmail(), userRegDTO.getCode());

            default: //除此之外的所有参数都会报错404
                return new ResultVO(StatusCode.URL_PARAMETER_ERROR);
        }
    }

    @PostMapping("/login/{type}")
    @ApiOperation("登录功能")
    public ResultVO login(@RequestBody UserLoginDTO userLoginDTO, @PathVariable Integer type) {
        log.debug("正在处理登录请求");

        switch (type) {
            case 1: // type为1时 处理用户名密码登录请求
                return userService.loginByUsername(userLoginDTO.getUsername(), userLoginDTO.getPassword());

            case 2: // type为2时 处理手机号密码登录请求
                return userService.loginByPhone(userLoginDTO.getPhone(), userLoginDTO.getPassword());

            case 3: // type为3时 处理邮箱密码登录请求
                return userService.loginByEmail(userLoginDTO.getEmail(), userLoginDTO.getPassword());

            case 4: // type为4时 处理手机号验证码登录
                return userService.loginByPhoneAndCode(userLoginDTO.getPhone(), userLoginDTO.getCode());

            case 5: // type为4时 处理邮箱验证码登录
                return userService.loginByEmailAndCode(userLoginDTO.getEmail(), userLoginDTO.getCode());

            default: //除此之外的所有参数都会报错404
                return new ResultVO(StatusCode.URL_PARAMETER_ERROR);
        }

    }

    @PostMapping("/logout")
    @ApiOperation("登出功能")
    public void logout() {
        SecurityContextHolder.clearContext();
    }

    @PutMapping("/update/detail")
    @ApiOperation("修改个人信息")
    public ResultVO update(@RequestBody UserDetailDTO userDetailDTO) {
        log.debug("正在处理修改个人详细信息请求, {}", userDetailDTO);

        userService.updateUserDetailById(userDetailDTO);
        return ResultVO.ok();
    }

    @PutMapping("/update/username")
    @ApiOperation("修改用户名")
    public ResultVO updateUsernameById(@RequestBody UserUpdateNameDTO userUpdateNameDTO) {
        log.debug("正在处理修改个人用户名的请求, {}", userUpdateNameDTO);

        return userService.updateUsernameById(userUpdateNameDTO);
    }

    @PutMapping("/update/pwd")
    @ApiOperation("修改密码")
    public ResultVO updatePasswordById(@RequestBody UserUpdatePwdDTO userUpdatePwdDTO) {
        log.debug("正在处理修改个人密码的请求, {}", userUpdatePwdDTO);

        return userService.updatePasswordById(userUpdatePwdDTO);
    }

    @PutMapping("/update/email")
    @ApiOperation("修改邮箱")
    public ResultVO updateEmailById(@RequestBody UserUpdateEmailDTO userUpdateEmailDTO) {
        log.debug("正在处理修改个人邮箱的请求, {}", userUpdateEmailDTO);

        return userService.updateEmailById(userUpdateEmailDTO);
    }

    @PutMapping("/update/phone")
    @ApiOperation("修改手机号")
    public ResultVO updatePhoneById(@RequestBody UserUpdatePhoneDTO userUpdatePhoneDTO) {
        log.debug("正在处理修改个人手机号的请求, {}", userUpdatePhoneDTO);

        return userService.updatePhoneById(userUpdatePhoneDTO);
    }

    @GetMapping("/code/phone/{phone}")
    @ApiOperation("发送手机验证码")
    public ResultVO sendCodeToPhone(@PathVariable String phone) {
        return userService.sendCodeToPhone(phone);
    }

    @GetMapping("/code/email/{email}")
    @ApiOperation("发送邮箱验证码")
    public ResultVO sendCodeToEmail(@PathVariable String email) {
        return userService.sendCodeToEmail(email);
    }



}



















